home *** CD-ROM | disk | FTP | other *** search
- /* viewio.c file - I/O module for VIEW program */
- #include "stdio.h"
- #include "cminor.h"
- #include "viewparm.h"
-
- /* fseek mode definitions */
- #define CURRENT_REL 1 /* relative to current position */
- #define EOF_REL 2 /* relative to end of the file */
- #define BOF_REL 0 /* relative to beginning of file*/
-
- #define CTL_Z 26 /* marks EOF in some text files */
-
- FILE *fp ; /* file pointer for text file */
- long filesize ; /* size of file in bytes */
- long int ftell() ;
- FILE *gfopen() ;
-
- int open_file(fn) /* open a file */
- char fn[] ; /* file name string */
- { /* return success or failure */
- fp = gfopen(fn,"r",BIN_MODE) ;
- if(fp != NULL)
- return(SUCCESS) ;
- else return(FAILURE) ;
- }
-
- int set_filesize(chk_cltz) /* set up filesize */
- int chk_cltz ; /* if = ASC_MODE check CONTROL-Z*/
- {
- long back ;
- int c ;
-
- fseek(fp,0L,EOF_REL) ; /* get size of file */
- filesize = ftell(fp) ;
-
- if( chk_cltz != ASC_MODE ) /* controll-z checking needed ? */
- return ;
-
- /* check last 128 byte block for a CTL_Z */
- back = filesize % 128 ; /* how for back to start of block*/
- if( (back == 0) && (filesize > 0) ) /* if at start of block */
- back = 128 ; /* last block is full */
- fseek(fp, filesize - back , BOF_REL) ;
- c = getc(fp) ;
- while( c != EOF )
- { if( c == CTL_Z ) /* look for control-Z */
- { filesize = ftell(fp) - 1 ;
- return ; /* found - adjust file size and exit */
- }
- c = getc(fp) ;
- }
- }
-
- int move_to(new_pos) /* move to specified position */
- long new_pos ;
- {
- fseek(fp,new_pos,BOF_REL) ;
- }
-
- long where_now() /* return current position */
- {
- return( ftell(fp) ) ;
- }
-
-
- int get_next_char() /* get char at file position */
- {
- char c ;
-
- if( ftell(fp) == filesize ) /* are we at end of file ? */
- return( EOF_MARK ) ; /* yes - return end of file mark*/
- else /* no - get a character */
- { c = getc(fp) ; /* get the character */
- return(toascii(c) ) ; /* force char to ascii, return it*/
- }
- }
-
-
- int get_previous_char() /* read the character in front */
- { /* of the current file position */
- char c ;
-
- if( ftell(fp) != 0L ) /* check for beginning of file */
- { fseek(fp,-1L,CURRENT_REL) ; /* back up one character */
- c = getc(fp) ; /* read the character */
- fseek(fp,-1L,CURRENT_REL) ; /* back up in front of char read*/
- return( toascii(c) ) ; /* force it to ASCII and return */
- }
- else return(BOF_MARK) ; /* it is beginning */
- }
-
-
- int close_file()
- {
- fclose(fp) ;
- }